#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "menu.h"
#include "test.h"
#include "strSpliter.h"

int TestHelp(int argc, char *argv[]);

COMMAND_TABLE CommandTable[] = {
    {
        "HELP",
        "List the command",
        "",
        TestHelp
    },
    {
        "RAND",
        "",
        "",
        TestRand
    },
    {
        "MULT",
        "",
        "",
        TestMultiple
    },
    {
        "PRIME",
        "",
        "",
        TestPrime
    },
    {
        "GOL",
        "",
        "",
        TestGoldbach
    },
    {
        "PSH1",
        "",
        "",
        TestPsh1
    },
    {
        "PSH2",
        "",
        "",
        TestPsh2
    },
    {
        "TOK",
        "Test My strtok function",
        "",
        TestMystrtok
    },
    {
        "PRO",
        "",
        "",
        TestProtocol
    },
    {
        "ARG",
        "Test the argument",
        "",
        TestArgs
    },
    {
        "MARGV",
        "Test the makeargv",
        "",
        Testargv
    },
    {
        "insertion", 
        "Test the insertion_sort", 
        "",
        TestInsertion_sort
    },
    {
        "spliter", 
        "Test the spliter for strings", 
        "spliter [string] [delimeter]",
        TestSpliter
    },
    {
        "sort",
        "Test the sort",
        "",
        Test_sort
    },
    {
        "quicksort",
        "Test the algorithm of quick sort",
        "",
        TestQuicksort
    },
    {
        "find_mini",
        "Find the order mini number of the array",
        "find_mini [order_number]",
        TestFindOrderNumber
    },
    {
        "bsearch",
        "Test the binary search",
        "bsearch [number]",
        TestBinaryserch
    },
    {
        "bsqrt",
        "Calculate the sqrt via binary search",
        "bsqrt [number(>0)]",
        TestBinarysqrt
    },
    {
        "stack",
        "A simple stack test. Print backward via stack",
        "stack",
        TestStack
    },
    {
        "printbackward",
        "Print backward via iterator",
        "printbackward word",
        TestPrintBackward
    },
    {
        "maze",
        "Test the maze via stack(DFS)",
        "maze",
        TestMaze
    },
    {
        "BFS",
        "Test the maze via queue(BFS)",
        "BFS",
        TestBFSMaze
    },
    {
        "trimtest",
        "Test my trim function",
        "trimtest",
        TestTrim
    },
    {
        "powerof2",
        "Test powerOf2",
        "powerOf2 n",
        TestPowerOf2
    },

};

int is_exit_cmd(const char *cmd)
{
    char str[80];

    int len = (sizeof(str) > (strlen(cmd) + 1) ? (strlen(cmd) + 1): (sizeof(str)));
    strncpy(str,cmd,len);

    TurnToupper(str);
    return strcmp(str,"EXIT");
}


int ExcuteCommand(const char *cmdStr)
{
    int i;  
    int status = 1;

    char str[80];

    // split the command list
    StrSpliter spliter = getStrSpliter();
    char **argv = spliter->split(spliter, cmdStr, " ");
    char *Command = argv[0];
    int argc = spliter->length(spliter) - 1;

    int length = LENGTH(CommandTable, COMMAND_TABLE);

    if (strcmp(Command,"") == 0) {
        spliter->free(spliter);
        return status;
    }
    int len = (sizeof(str) > (strlen(Command) + 1) ?
            (strlen(Command) + 1): (sizeof(str)));
    strncpy(str,Command,len);
    TurnToupper(str);
    for (i=0;i<length;i++){
        char test_cmd[80];
        len = (sizeof(test_cmd) > (strlen(CommandTable[i].Cmd) + 1) ?
                (strlen(CommandTable[i].Cmd) + 1): \
                (sizeof(test_cmd)));
        strncpy(test_cmd, CommandTable[i].Cmd, len);
        TurnToupper(test_cmd);

        if (str_equ(test_cmd,str)){
            status = CommandTable[i].Function(argc, &argv[1]);  // error:-1,success:0,unexecute:1
            break;
        }
    }
    if (status == 1) {
        printf("Can not find the command: \"%s\".\nEnter 'help' to get the usage info.\n",Command);
    }
    printf(":-)");

    spliter->free(spliter);
    return status;
 }


int TestHelp(int argc, char *argv[])
{
    int suffix;
    int length = LENGTH(CommandTable, COMMAND_TABLE);

    printf("\n%-15s%s","Command","Description\n");

    for (suffix=0;suffix<length;suffix++) {
        printf("%-15s%s\n  Usage:\n       %s\n",CommandTable[suffix].Cmd, CommandTable[suffix].description,
                CommandTable[suffix].help);
    }
    return 0;
}
